package com.majun.base.operate;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * * hasElement：判断Mono是否包含元素；
 * 
 * hasElements：判断Flux是否包含元素；
 * 
 * hasElementWith：判断Mono是否包含与给定Predicate匹配的元素；
 * 
 * all：判断Flux中的所有元素是否都满足给定的条件；
 * 
 * any：判断Flux中是否有任意一个元素满足给定的条件；
 * 
 * isEmpty：判断Flux是否为空；
 * 
 * switchIfEmpty：如果Mono或Flux为空，则切换到另一个Mono或Flux；
 * 
 * defaultIfEmpty：如果Mono或Flux为空，则返回默认值；
 */
public class CombinationExample {
	public static void main(String[] args) {
		// 示例 1: 使用 Mono 组合操作符
		Mono<String> mono1 = Mono.just("Hello");
		Mono<String> mono2 = Mono.just("World");
		Mono<String> combined = mono1.zipWith(mono2, (a, b) -> a + " " + b);
		combined.subscribe(System.out::println); // 输出: Hello World

		Mono<Void> when = Mono.when(mono1, mono2);
		when.subscribe(null, Throwable::printStackTrace, () -> System.out.println("Completed")); // 输出: Completed

		// 示例 2: 使用 Flux 组合操作符
		Flux<String> flux1 = Flux.just("A", "B", "C");
		Flux<String> flux2 = Flux.just("1", "2", "3");
		Flux<String> merged = Flux.merge(flux1, flux2);
		merged.subscribe(System.out::println); // 输出: A 1 B 2 C 3

		Flux<String> concatenated = Flux.concat(flux1, flux2);
		concatenated.subscribe(System.out::println); // 输出: A B C 1 2 3

		Flux<String> zipped = Flux.zip(flux1, flux2, (a, b) -> a + b);
		zipped.subscribe(System.out::println); // 输出: A1 B2 C3

		Flux<String> combinedLatest = Flux.combineLatest(flux1, flux2, (a, b) -> a + b);
		combinedLatest.subscribe(System.out::println); // 输出: C3

		Flux<String> started = flux1.startWith("Start");
		started.subscribe(System.out::println); // 输出: Start A B C
	}
}
